import { constantRoutes, asyncRoutes } from '@/router'

const state = () => {
  return {
    // 作用: 用于控制左侧菜单的展示
    routes: constantRoutes
  }
}

const mutations = {
  setRoutes(state, newRoutes) {
    // 这一步是: 画龙点睛之笔
    state.routes = [...constantRoutes, ...newRoutes]
  }
}

const actions = {
  filterRoutes(ctx, menu) {
    const routes = []
    menu.forEach(key => {
      // routes.push(...asyncRoutes.filter(item => item.name === key))
      routes.push(...asyncRoutes.filter(item => item.children[0].name === key))
    })
    console.log('.......')
    console.log(routes)
    ctx.commit('setRoutes', routes) // 是用于左侧菜单的遍历展示的
    return routes // 是用于路由的动态追加的 addRoutes()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
